package com.sise.rsc.controller;

import com.sise.rsc.bean.Result;
import com.sise.rsc.entity.Courier;
import com.sise.rsc.entity.Role;
import com.sise.rsc.entity.User;
import com.sise.rsc.entity.WorkingArea;
import com.sise.rsc.service.AdminService;
import com.sise.rsc.service.RoleService;
import com.sise.rsc.service.UserService;
import com.sise.rsc.util.RandomUtil;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.OptionalInt;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private AdminService adminService;
    @Resource
    private RoleService roleService;
    @Resource
    private UserService userService;
    @Resource
    private PasswordEncoder passwordEncoder;

    @RequestMapping("/index")
    public String index(){

        return "admin/index";
    }

    @RequestMapping("/addCourierView")
    public String addCourier(Model model){
        //查询所有工作区域
        List<WorkingArea> workingAreas = adminService.findAllWorkingArea();
        //如果没有工作区域，则重定向到添加工作区域界面
        if (workingAreas.isEmpty())
            return "redirect:admin/addWorkingAreaView";
        //加入model
        model.addAttribute("workingAreas",workingAreas);
        return "admin/add-courier";
    }
    @RequestMapping("/addCourier")
    @ResponseBody
    public Result addCourier(String name, String phone, String username, String password, String address, String sex, String workingAreaId){

        User user0 = userService.findUserByUsername(username);
        if (user0!=null)
            return new Result(500,"登录名称已经存在！");


//        {"realName":"cailong ","phone":"1343434","username":"123","password":"456","address":"华软","sex":"男","workingAreaId":"2"}
        User user = new User();
        user.setUsername(username);
        user.setPassword(passwordEncoder.encode(password));
        user.setPhone(phone);

        Role role = roleService.findRoleByAuthority("ROLE_KD");
        if (role == null){
            role = new Role("ROLE_KD");
            roleService.save(role);
        }
        List<Role> roles = new LinkedList<>();
        roles.add(role);
        user.setRoles(roles);
        userService.save(user);

        Courier courier = new Courier();
        String no;
        while (true){
            no ="Y"+ RandomUtil.getRandomNumber(4);
            if (adminService.findCourierByNo(no) ==null)
                break;
        }
        courier.setNo(no);
        courier.setRealName(name);
        courier.setSex(sex);
        WorkingArea workingArea = adminService.findWorkingAreaById(Integer.parseInt(workingAreaId));
        if (workingArea == null)
            return new Result(500,"工作区域错误！");
        courier.setWorkingArea(workingArea);
        courier.setAddress(address);
        courier.setUser(userService.findUserByUsername(username));


        adminService.addCourier(courier);
        return new Result(200,"处理成功！");
    }

    @RequestMapping("/addWorkingAreaView")
    public String addWorkingArea(){



        return "admin/add-working-area";
    }


    @RequestMapping("/addWorkingArea")
    @ResponseBody
    public Result addWorkingArea(String address){

        if (address.equals(""))
            return new Result(500,"地址信息不能为空！");

        WorkingArea workingArea = adminService.findWorkingAreaByAddressName(address);

        if (workingArea != null)
            return new Result(500,"地址["+address+"]已存在，请重新输入！");
        else {

            workingArea = new WorkingArea();
            workingArea.setAddressName(address);
            adminService.addWorkingAres(workingArea);
            return new Result(200,"地址信息添加成功！");
        }



    }
}
